package application;


import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;

import jeopardy.common.protocol.game.stats.ShowGameListReqMsg;
import jeopardy.common.protocol.game.stats.ShowGameListResMsg;
import jeopardy.common.protocol.useraccount.UserAccountConsts;
import jeopardy.common.protocol.useraccount.UserAccountRequest;
import jeopardy.common.protocol.useraccount.UserAccountUtils;

public class PreGameScreenFetchData implements Runnable {

    public UserSession sessionU;
    public PreGameScreenSession sessionG;

    public String userName;
    public char[] passWord;
    public int authenticated;
    public int sockType = 2;
    public static List<String> myAvailableGames;
      
	private static int connected;
	
	public int loopCounter = 0;
	public int maxIteration = 10;

	String SERVER_IP; 
	static int SERVER_PORT;
	private static SSLSocket statSocket;
	public static byte[] returnCode;
	
    
    private String msg;
    

	public PreGameScreenFetchData()
    {
		authenticated = 0;
		connected = 0;
		passWord = null;
		userName = null;
		SERVER_IP = null;
		SERVER_PORT = -1;
		returnCode = new byte[UserAccountConsts.USERACCOUNT_MAX_RES_LEN];
		sessionU = UserSession.getInstance();
		sessionG = PreGameScreenSession.getInstance();	
    }
    
	/*
	 * This method is used to create socket to communicate with the server
	 */
	public static int createSocket(int type)
	{
		// byte[] returnCode = new byte[UserAccountConsts.USERACCOUNT_MAX_RES_LEN];
		switch(type) {
			
			case 1: SERVER_PORT = ClientConsts.SERVER_PORT_USER_ACCOUNT;
					break;
			case 2: SERVER_PORT = ClientConsts.SERVER_PORT_STATS;
					break;
			case 3: SERVER_PORT = ClientConsts.SERVER_PORT_GAME;
					break;
			default: SERVER_PORT = -9000;
					break;
		}
		try {
			SSLSocketFactory sslsocketfactory = ClientConsts.createSSLFactory();
			statSocket = (SSLSocket) sslsocketfactory.createSocket(
						ClientConsts.SERVER_IP, SERVER_PORT);
			connected = 1;
			return 0;
			
		} catch (Exception exception) {
			exception.printStackTrace();
			System.out.println("Failed to create socket");
			connected = 0;
	        return 1;
		}	
	}
	
	// Not necessary - redundant 
	public static void copyLists(List<String> destList)
	{
		destList = new ArrayList<String>(myAvailableGames);
		System.out.println("FetchGameListAndStats::copyList - copying list");
		if(myAvailableGames.size() > 0) {
			for (int i=0; i < myAvailableGames.size(); i++) {
				String element = myAvailableGames.get(i);
				System.out.println(element);
			}
		}
	}
	
	public void run() {
		// Loop twenty times
		while( ((authenticated = sessionU.getStatus())== 0) && (loopCounter < maxIteration) ) { 
			try {
				System.out.println("Thread(FetchGameListAndStats) -  Going back to sleep");
				Thread.sleep(4000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			maxIteration++;
		}
		if(authenticated == 1) {
			System.out.println("Authenticated - connecting the server");
			
			createSocket(sockType);
			if(statSocket.isConnected()) {
				System.out.println("FetchGameListAndStats - connected to the server");
			}
			/*
			sessionG.myAvailableGames.add("Adam");
			sessionG.myAvailableGames.add("Bob");
			sessionG.myAvailableGames.add("Charlie");
			sessionG.myAvailableGames.add("David");
			sessionG.myAvailableGames.add("Erin");
			*/
			
			try {
				ShowGameListReqMsg newReq = new ShowGameListReqMsg(sessionU.userName, sessionU.password);
			
				ObjectOutputStream newOut = new ObjectOutputStream(statSocket.getOutputStream());
				newOut.writeObject(newReq);
				ObjectInputStream newIn = new ObjectInputStream(statSocket.getInputStream());
				ShowGameListResMsg response = (ShowGameListResMsg) newIn.readObject();
				
				String[] games = response.getWaiting();
				for(String game : games) {
					sessionG.myAvailableGames.add(game);
				}
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
		//Print list of games
		System.out.println("Printing list of games available");
		sessionG.printList();

	}
	
}
	
